/**
 * messager - jQuery xui
 *
 * Licensed under the GPL:
 *   http://www.gnu.org/licenses/gpl.txt
 *
 * Copyright 2013 xjb [ beymy.en@gmail.com ]
 *
 * 依赖
 *	dialog
 */
(function($) {
    /**
     * 动画显示对话框,超时自动关闭
     */
    function show(dlg, type, speed, timeout) {
        if (!dlg) return;
		
		dlg.css('z-index', $.fn.dialog.defaults.zIndex++);
        switch (type) {
            case null:
                dlg.show();
                break;
            case 'slide':
                dlg.slideDown(speed);
                break;
            case 'fade':
                dlg.fadeIn(speed);
                break;
            case 'show':
                dlg.show(speed);
                break;
        }

        var timer = null;
        if (timeout > 0) {
            timer = setTimeout(function() {
                hide(dlg, type, speed);
            }, timeout);
        }
        dlg.hover(
            function() {
                if (timer) {
                    clearTimeout(timer);
                }
            },
            function() {
                if (timeout > 0) {
                    timer = setTimeout(function() {
                        hide(dlg, type, speed);
                    }, timeout);
                }
            }
        )
    }

    /**
     * 动画隐藏对话框
     */
    function hide(dlg, type, speed) {
        if (!dlg) return;

        switch (type) {
            case null:
                dlg.hide();
                break;
            case 'slide':
                dlg.slideUp(speed);
                break;
            case 'fade':
                dlg.fadeOut(speed);
                break;
            case 'show':
                dlg.hide(speed);
                break;
        }

        setTimeout(function() {
            dlg.remove();
        }, speed);
    }

    /**
     * 构建对话框
     */
    function buildDialog(title, content, buttons) {
        var dlg = $('<div class="messager-body"></div>').appendTo('body');
        dlg.append(content);
        if (buttons) {
            var tb = $('<div class="messager-button"></div>').appendTo(dlg);
            for (var label in buttons) {
                $('<a href="javascript:void(0)"></a>').text(label).css('margin-left', 10).bind('click', buttons[label]).appendTo(tb).linkbutton();
            }
        }
        dlg.dialog({
            title: title,
            width: 300,
            height: 'auto',
            modal: true,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            resizable: false,
            onClose: function() {
                setTimeout(function() {
                    dlg.dialog('destroy');
                }, 100);
            }
        });
        return dlg;
    }

    $.messager = {
        show: function(options) {
            var opts = $.extend({
                showType: 'slide',
                showSpeed: 600,
                width: 250,
                height: 100,
                msg: '',
                title: '',
                timeout: 4000
            }, options || {});

            var dlg = $('<div class="messager-body"></div>').html(opts.msg).appendTo('body');
            dlg.dialog({
                title: opts.title,
                width: opts.width,
                height: opts.height,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                shadow: false,
                closed: true,
                onBeforeOpen: function() {
                    show($(this).dialog('dialog'), opts.showType, opts.showSpeed, opts.timeout);
                    return false;
                },
                onBeforeClose: function() {
                    hide(dlg.dialog('dialog'), opts.showType, opts.showSpeed);
                    return false;
                }
            });

            //附加样式
            if (opts.style) {
                dlg.dialog('dialog').css(opts.style);
            }
            dlg.dialog('open');
        },

        alert: function(title, msg, icon, fn) {
            var content = '<div>' + msg + '</div>';
            switch (icon) {
                case 'error':
                    content = '<div class="messager-icon messager-error"></div>' + content;
                    break;
                case 'info':
                    content = '<div class="messager-icon messager-info"></div>' + content;
                    break;
                case 'question':
                    content = '<div class="messager-icon messager-question"></div>' + content;
                    break;
                case 'warning':
                    content = '<div class="messager-icon messager-warning"></div>' + content;
                    break;
            }
            content += '<div style="clear:both;"/>';

            var buttons = {};
            buttons[$.messager.defaults.ok] = function() {
                dlg.dialog('close');
                if (fn) {
                    fn();
                    return false;
                }
            };
            var dlg = buildDialog(title, content, buttons);
        },

        confirm: function(title, msg, fn) {
            var content = '<div class="messager-icon messager-question"></div>' + '<div>' + msg + '</div>' + '<div style="clear:both;"/>';
            var buttons = {};
            buttons[$.messager.defaults.ok] = function() {
                dlg.dialog('close');
                if (fn) {
                    fn(true);
                    return false;
                }
            };
            buttons[$.messager.defaults.cancel] = function() {
                dlg.dialog('close');
                if (fn) {
                    fn(false);
                    return false;
                }
            };
            var dlg = buildDialog(title, content, buttons);
        },

        prompt: function(title, msg, fn) {
            var content = '<div class="messager-icon messager-question"></div>' + '<div>' + msg + '</div>' + '<br/>' + '<input class="messager-input" type="text"/>' + '<div style="clear:both;"/>';
            var buttons = {};
            buttons[$.messager.defaults.ok] = function() {
                dlg.dialog('close');
                if (fn) {
                    fn($('.messager-input', dlg).val());
                    return false;
                }
            };
            buttons[$.messager.defaults.cancel] = function() {
                dlg.dialog('close');
                if (fn) {
                    fn();
                    return false;
                }
            };
            var dlg = buildDialog(title, content, buttons);
        }
    };

    $.messager.defaults = {
        ok: '确认',
        cancel: '取消'
    };

})(jQuery);
